package cn.hn.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Element;

/**
 * 
 * 小工具
 * @author shenhao
 * 
 */
public class Kit {

	public static void main(String[] args)
	{
		String html=httpRequest("http://www.xeixe.com/home.php?mod=space&do=poll&view=me",null,
				"GET","GBK");
		System.out.println(html);
	}
	
	/*
	 * (non-Javadoc) <p>Title: checkIllTag</p> <p>Description: 检测有误导的标签</p>
	 * 
	 * @param content
	 * 
	 * @return
	 */
	public static String checkIllTag(String content) {
		if (content != null) {
			String strBack = content;
			String[] strIllTag = { "&", "<", ">"," " };
			String[] strReplaceTag = { "&amp;", "&lt;", "&gt;" ,"&nbsp;"};
			for (int i = 0; i < strIllTag.length; i++) {
				strBack = strBack.replaceAll(strIllTag[i], strReplaceTag[i]);
			}
			return strBack;
		}
		return null;
	}

	/**
	 * 
	 * 
	 * @param URL
	 * 			
	 * @param requestContent
	 *            请求内容
	 * @param requestMethod
	 *            POST/GET
	 * @param charset
	 * 			  编码集
	 * @return
	 */
	public static String httpRequest(String URL, String requestContent,
			String requestMethod,String charset) {
		String strScript = null;
		try {
			URL url = new URL(URL);

			HttpURLConnection httpUrlConn = (HttpURLConnection) url
					.openConnection();
			httpUrlConn.setRequestProperty("User-agent","Mozilla/4.0");
			//默认120秒超时
			httpUrlConn.setConnectTimeout(120000);
			httpUrlConn.setReadTimeout(120000);
			httpUrlConn.setRequestMethod(requestMethod);
			httpUrlConn.setDoOutput(true);
			if (requestContent != null) {
				httpUrlConn.getOutputStream().write(requestContent.getBytes());
				httpUrlConn.getOutputStream().flush();
				httpUrlConn.getOutputStream().close();
			}
			InputStream is = httpUrlConn.getInputStream();

			byte[] arrRecv = getBytes(is, 9999);

			strScript = new String(arrRecv, charset);

			is.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return strScript;
	}

	/**
	 * 读取byte[]
	 * 
	 * @param inputstream
	 *            InputStream
	 * @return byte[]
	 * @throws IOException
	 */
	public static byte[] getBytes(InputStream inputstream, int iBufferSize)
			throws IOException {
		byte[] bArr = new byte[iBufferSize];

		int iIndex = 0;
		int iLength = 0;
		int iTotalLength = 0;

		while ((iLength = inputstream.read(bArr, iIndex, bArr.length - iIndex)) != -1) {
			iIndex += iLength;
			if (iLength != -1) {
				iTotalLength += iLength;
			}
			if (iIndex >= bArr.length) {
				byte[] arrbTemp = new byte[bArr.length];
				System.arraycopy(bArr, 0, arrbTemp, 0, bArr.length);
				bArr = null;
				bArr = new byte[arrbTemp.length + iBufferSize];
				System.arraycopy(arrbTemp, 0, bArr, 0, arrbTemp.length);
				arrbTemp = null;
			}
		}
		byte[] bBack = new byte[iTotalLength];

		System.arraycopy(bArr, 0, bBack, 0, bBack.length);

		bArr = null;

		System.gc();
		return bBack;
	}
	

}
